/**
 * 比较两次选中的 sales 是否相等
 * @param arr1
 * @param arr2
 * @returns {boolean}
 *
 *                     [
 *                        {attrId: 1, attrValue: ['黑色','白色']},
 *                        {attrId: 2, attrValue: ['128GB','256GB']}
 *                     ]
 */
const salesEqual = (arr1, arr2) => {
  if (arr1 === undefined) {
    return false
  }

  if (arr1.length !== arr2.length) {
    return false
  }

  for (let i = 0; i < arr1.length; i++) {
    const subArr1 = arr1[i]
    const subArr2 = arr2[i]

    if (subArr1.attrId !== subArr2.attrId) {
      return false
    }

    if (subArr1.attrValue.length !== subArr2.attrValue.length) {
      return false
    }

    for (let j = 0; j < subArr1.attrValue.length; j++) {
      if (subArr1.attrValue[j] !== subArr2.attrValue[j]) {
        return false
      }
    }
  }

  return true
}

/**
 * 校验表单是否更改过
 * @param remember
 * @param sales
 * @param baseForm
 * @returns {boolean}
 */
const rememberEqual = (remember, sales, baseForm) => {
  if (remember && salesEqual(remember.sales, sales)) {
    let form = remember.form
    if (form) {
      return form.spuName === baseForm.spuName
        && form.title === baseForm.title
        && form.subTitle === baseForm.subTitle
        && form.imgs === baseForm.imgs
    }
  }

  return false
}

export { rememberEqual }
